<?php
declare (strict_types = 1);

namespace app\Api\controller;

use app\Api\model\Sign;
use app\Api\model\SignConfig;
use app\Api\model\User;
use think\Db;
use think\Request;

class SignController
{
    protected $sign_config;
    function __construct()
    {
      //查询开启的签到配置
      $this->sign_config=SignConfig::detailsSignConfig();
    }
    //前台用户签到
    public function signList()
    {

        //用户id  session取 未作登录暂时模拟
        $user_id=1;
        //查询用户信息
        $user_data=User::userDetails($user_id);
        //本周开始时间戳
        $start_time = mktime(0, 0, 0, (int)date('m'), date('d') - date('w') + 1, (int)date('Y'));
        $beginToday=mktime(0,0,0,(int)date('m'),(int)date('d'),(int)date('Y'));
        $endToday=mktime(0,0,0,(int)date('m'),(int)date('d')+1,(int)date('Y'))-1;
        //查询本周签到总信息
        $sign_record=Sign::signRecord($user_id,$start_time);
        //判断今天星期几  星期一 直接查询上一天的签到数据是否有
        $weak=date("w");
        if (!$weak==1){
            $beginYesterday=$start_time-60*60*24;
            $sign_details=Sign::signDetails($user_id,$start_time,$beginYesterday);
            $res=Sign::signDetails($user_id,$beginToday,$endToday);
            if (!$sign_details){
                if ($res){
                $data=[1,0,0,0,0,0,0];
                return json(['code'=>0,'msg'=>'非连续签到今天已签到','data'=>$data]);
                }else{
                $data=[0,0,0,0,0,0,0];
                return json(['code'=>0,'msg'=>'非连续签到今天未签到','data'=>$data]);
                }

            }else{
                if ($res){
                    $data=[1,0,0,0,0,0,0];
                    return json(['code'=>0,'msg'=>'连续签到','data'=>$data]);
                }else{
                    $data=[0,0,0,0,0,0,0];
                    return json(['code'=>0,'msg'=>'连续签到今天未签到','data'=>$data]);
                }
            }
            }else{
           // return  json($sign_record);
             $datas=self::getSignDay($sign_record);
             return  json($datas);
           }
    }
    public static function getSignDay($sign_record)
    {
        $data=['first_day'=>0,'second_day'=>0,'third_day'=>0,'fourth_day'=>0,'fifth_day'=>0,'sixth_day'=>0,'seventh_day'=>0];
        foreach ($sign_record as $k=>$v){
          $ff=date('w',strtotime ($v['create_time']));

            switch ($ff){
                case 1:
                    $data['first_day']=1;
                    break;
                case 2:
                    $data['second_day']=1;
                    break;
                case 3:
                    $data['third_day']=1;
                    break;
                case 4:
                    $data['fourth_day']=1;
                    break;
                case 5:
                    $data['fifth_day']=1;
                    break;
                case 6:
                    $data['sixth_day']=1;
                    break;
                default:
                    $data['seventh_day']=1;
            }
        }
        return $data;
    }
    public  function checkSign()
    {
        //用户id  session取 未作登录暂时模拟
        $user_id=1;
        //查询用户信息
        $user_data=User::userDetails($user_id);
        //本周开始时间戳
        $beginToday=mktime(0,0,0,(int)date('m'),(int)date('d'),(int)date('Y'));
        $endToday=mktime(0,0,0,(int)date('m'),(int)date('d')+1,(int)date('Y'))-1;
        $beginYesterday=mktime(0,0,0,(int)date('m'),(int)date('d')-1,(int)date('Y'));
        $endYesterday=mktime(0,0,0,(int)date('m'),(int)date('d'),(int)date('Y'))-1;
        $weak=date("w");
        //判断今天是否签到
        $res=Sign::signDetails($user_id,$beginToday,$endToday);
        if ($res){
            return json(['code'=>1,'msg'=>'已签到请勿重复签到','data'=>null]);
        }else{

            $pream=Sign::signDetails($user_id,$beginYesterday,$endYesterday);
            if($pream){
                //连续签到
                $where['user_id']=1;
                $where['is_sign']=1;
                $where['integral']=$this->getSignReward($user_data['signs']);
                $user_info['signs']=$user_data['signs']+1;
                $user_info['points']=$user_data['points']+$where['integral'];
                $user_info['is_sign']=1;
                Db::startTrans();
                try {
                    Sign::createSign($where);
                    User::updateUser($user_id,$user_info);
                    Db::commit();
                    return json(['code'=>0,'msg'=>'连续签到成功','data'=>null]);
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    return json(['code'=>1,'msg'=>'连续签到失败','data'=>null]);
                }
            }else{
                //今天为第一天签到
                $where['user_id']=1;
                $where['is_sign']=0;
                $where['integral']=$this->sign_config['first_day'];
                $user_info['signs']=1;
                $user_info['points']=$user_data['points']+$this->sign_config['first_day'];
                $user_info['is_sign']=0;
                Db::startTrans();
                try {
                    Sign::createSign($where);
                    User::updateUser($user_id,$user_info);
                    Db::commit();
                    return  '签到成功';
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    return  '签到失败';
                }


            }
        }
    }
    //签到奖励
    public  function getSignReward($signs)
    {
            switch ($signs){
                case 1:
                    $integral=$this->sign_config['first_day'];
                    break;
                case 2:
                    $integral=$this->sign_config['second_day'];
                    break;
                case 3:
                    $integral=$this->sign_config['third_day'];
                    break;
                case 4:
                    $integral=$this->sign_config['fourth_day'];
                    break;
                case 5:
                    $integral=$this->sign_config['fifth_day'];
                    break;
                case 6:
                    $integral=$this->sign_config['sixth_day'];
                    break;
                default:
                    $integral=$this->sign_config['seventh_day'];
            }

        return $integral;
    }


}
